﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class RoleService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Role> getList()
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Role.Where(o=>o.State==true).AsQueryable();
 
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        public List<T_Role_List> getComboxList()
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Role_List> list = db.T_Role.Where(o => o.State != false)
                        .Select(o => new T_Role_List { ID = o.ID, RoleName = o.RoleName }).ToList(); 
                    
                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Role tu, List<T_Permission> perList)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        var user = db.T_Role.Where(o => o.RoleName == tu.RoleName).FirstOrDefault();
                        if (user != null)
                        {
                            throw new ApplicationException("角色信息已存在!");
                        }
                        tu.State = true;
                        tu.CreatedTime = GetSqlNow();
                        tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                        db.T_Role.Add(tu);
                        db.SaveChanges();

                        List<T_Role_Permission> rpList = new List<T_Role_Permission>();
                        T_Role_Permission rp = new T_Role_Permission();
                        //只取选中的记录
                        perList = perList.Where(o => o.IsChecked == true).ToList();
                        foreach (var per in perList)
                        {
                            rp = new T_Role_Permission();
                            rp.Role_ID = tu.ID;
                            rp.Permission_ID = per.ID;
                            //rp.Permission_Code = per.PermissionCode;
                            rpList.Add(rp);
                        }
                        db.T_Role_Permission.AddRange(rpList);
                        db.SaveChanges();

                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Role tu, List<T_Permission> perList)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        var item = db.T_Role.Where(o => o.ID == tu.ID ).FirstOrDefault();
                        if (item == null)
                        {
                            throw new ApplicationException("角色信息不存在!");
                        }
                        item.RoleName = tu.RoleName; 
                        item.ModifiedTime = GetSqlNow();
                        item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                        db.SaveChanges();

                        //先删除再新增
                        var delList = db.T_Role_Permission.Where(o => o.Role_ID == tu.ID).ToList();
                        if(delList.Count>0)
                        { 
                            db.T_Role_Permission.RemoveRange(delList);
                            db.SaveChanges();
                        } 
                        List<T_Role_Permission> rpList = new List<T_Role_Permission>();
                        T_Role_Permission rp = new T_Role_Permission();
                        //只取选中的记录
                        perList = perList.Where(o => o.IsChecked == true).ToList();
                        foreach (var per in perList)
                        {
                            rp = new T_Role_Permission();
                            rp.Role_ID = tu.ID;
                            rp.Permission_ID = per.ID;
                            //rp.Permission_Code = per.PermissionCode;
                            rpList.Add(rp);
                        }
                        db.T_Role_Permission.AddRange(rpList);
                        db.SaveChanges();

                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Role item)  //List<T_Role> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                   
                        var fact = db.T_Role.Where(o => o.ID == item.ID ).FirstOrDefault();
                        if (fact == null)
                        {
                            throw new ApplicationException("角色信息不存在!");
                        }
                        fact.State = false;
                        fact.DeletedTime = GetSqlNow();
                        fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                     
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }
 

    }
}
